package com.interview.client;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.interview.properties.AliyunOSSProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zzw
 * @date 2025/6/15
 * @description 阿里云OSS客户端
 */
@Slf4j
@Component
public class AliyunOSSClient {

    @Autowired
    OSS ossClient;

    @Autowired
    AliyunOSSProperties aliyunOSSProperties;

    /**
     * 上传文件
     *
     * @param objectName  文件名
     * @param inputStream 文件流对象
     * @return 文件访问地址（全路径）
     */
    public String upload(String objectName, InputStream inputStream) {
        //文件读取路径
        String url;
        // 判断文件
        if (inputStream == null) {
            log.error("上传文件：objectName{}文件流为空", objectName);
            return null;
        }
        log.info("OSS文件上传开始：{}", objectName);
        try {
            String bucketName = aliyunOSSProperties.getBucketName();
            // 上传文件
            PutObjectRequest request = new PutObjectRequest(bucketName, objectName, inputStream);

            PutObjectResult result = ossClient.putObject(request);
            // 设置权限(公开读)
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            if (result != null) {
                log.info("OSS文件上传成功：{}", objectName);
            }
        }
        catch (OSSException oe) {
            log.error("OSS文件上传错误:{}", oe.getMessage());
        }
        catch (ClientException ce) {
            log.error("OSS文件上传客户端错误:{}", ce.getMessage());
        }

        url = aliyunOSSProperties.getUrlPrefix() + objectName;
        return url;
    }

    /**
     * 根据url删除文件
     *
     * @param pathUrl url地址（全路径）
     */
    public void delete(String pathUrl) {
        String prefix =
                "https://" + aliyunOSSProperties.getBucketName() + "." + aliyunOSSProperties.getEndpoint() + "/";
        String key = pathUrl.replace(prefix, "");
        List<String> keys = new ArrayList<>();
        keys.add(key);
        // 删除Objects
        ossClient.deleteObjects(new DeleteObjectsRequest(aliyunOSSProperties.getBucketName()).withKeys(keys));
    }
}
